<?php

namespace Trawind\Extension\Transformers\Custom;

use Trawind\Basics\TrawindTransformer;
use Trawind\Remotes\Repositories\Extension\Custom\ViewRepositoryEloquent;
use Trawind\Remotes\Repositories\Extension\Custom\FormSublistRepositoryEloquent;

class CustomViewTransformer extends TrawindTransformer
{

    use CustomTransformer;


    protected function getDetails()
    {
        $mode = request()->input('mode', 'table');
        if ($mode === 'table') {
            $viewId = request()->input('view_id', 0);
            $details = app(ViewRepositoryEloquent::class)
                ->details($this->businessTypeId, $viewId);
        } else {
            $sublistId = request()->input('sublist_id', 0);
            $details = app(FormSublistRepositoryEloquent::class)
                ->details($this->businessTypeId, $sublistId);
        }

        [$remoteBusinessType, $localBusinessType] = $this->collectWithParam($details);
        return [
            'fields' => array_column($details['fields'], 'field'),
            'sort_field' => [
                'sort_field_id' => $details['sort_field_id'],
                'sort_is_asc' => $details['sort_is_asc'],
                'sort_field' => $details['sort_field'],
            ],
            'searches' => $details['searches'] ?? [],
            'filter_fields' => $details['filter_fields'] ?? [],
            'localBusinessType' => $localBusinessType,
            'remoteBusinessType' => $remoteBusinessType,
        ];
    }


    public function diyFillTransformData(array $data)
    {
        $data['data'] = $this->buildRelationFieldToData($data['data']);
        $data['fields'] = $this->fields;
        $data['searches'] = $this->details['searches'];
        $data['filter_fields'] = $this->details['filter_fields'];
        return $data;
    }

}